import { BrowserWindow, ipcMain } from 'electron'
import { hackFakeCloseMainWindow, onErrorMainWindow } from 'app/events'
import { BrowserWindowExtend, IpcEvent, WindowOption } from 'app/type'
import url from 'url'
import path from 'path'

export class WindowCenter {
    private static map: Map<string, BrowserWindow> = new Map()

    static create(name: string, options: Electron.BrowserWindowConstructorOptions) {
        const win = new BrowserWindow(options) as BrowserWindowExtend
        win.name = name
        WindowCenter.map.set(name, win)
        // 如果创建的是主窗口，实现假关闭，并监听主进程错误
        if (name === 'main') {
            hackFakeCloseMainWindow(win)
            onErrorMainWindow(win)
        } else {
            win.on('closed', () => WindowCenter.map.delete(name))
        }
        return win
    }

    static getWindow(name: string) {
        return WindowCenter.map.get(name)
    }

    static restoreWindow(name: string) {
        const win = WindowCenter.getWindow(name)
        win?.restore()
        win?.show()
    }

    static closeWindow(name: string) {
        const win = WindowCenter.getWindow(name)
        win?.close()
    }

    static closeAllWindow() {
        for (const win of WindowCenter.map) {
            win[1].close()
        }
    }

    static init() {
        // 创建窗口
        ipcMain.handle(IpcEvent.APP_WINDOW_CREATE, (e, option: WindowOption) => {
            const existedWindow = WindowCenter.getWindow(option.name)
            if (existedWindow) {
                existedWindow.show()
                return
            }
            const win = WindowCenter.create(option.name, option.options)
            const winUrl = option.pathname.startsWith('http')
                ? option.pathname
                : url.format({
                      protocol: 'file:',
                      pathname: path.resolve(
                          __dirname,
                          '..',
                          '..',
                          'output',
                          'pages',
                          option.pathname,
                      ),
                      slashes: true,
                  })
            win.loadURL(winUrl)
        })

        // 关闭窗口
        ipcMain.handle(IpcEvent.APP_WINDOW_CLOSE, (e, name?: string) => {
            if (name) {
                const ewin = WindowCenter.getWindow(name)
                ewin?.close()
                return
            }
            const win = BrowserWindow.fromWebContents(e.sender)
            win?.close()
        })

        // 最大化窗口
        ipcMain.handle(IpcEvent.APP_WINDOW_MAXIMIZE, (e, name?: string) => {
            if (name) {
                const ewin = WindowCenter.getWindow(name)
                ewin?.isMaximized() ? ewin?.unmaximize() : ewin?.maximize()
                return
            }
            const win = BrowserWindow.fromWebContents(e.sender)
            win?.isMaximized() ? win?.unmaximize() : win?.maximize()
        })

        // 最小化窗口
        ipcMain.handle(IpcEvent.APP_WINDOW_MINIMIZE, (e, name?: string) => {
            if (name) {
                const ewin = WindowCenter.getWindow(name)
                if (ewin?.isMinimized()) {
                    ewin?.restore()
                    ewin?.show()
                } else {
                    ewin?.minimize()
                }
                return
            }
            const win = BrowserWindow.fromWebContents(e.sender)
            win?.isMinimized() ? win?.restore() : win?.minimize()
        })
    }
}
